﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Linq;
using System.Security.Claims;
using WebAPITest.Models.Database;
using WebAPITest.service;
using WebAPITest.Service;

namespace WebAPITest.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class AuthenticationController : ControllerBase
    {
        //注入jwt服务
        private readonly IJWTService _jwtService;
        private readonly IUserService _userService;
        private readonly WebEnterpriseIIContext _db;
        /// <summary>
        /// 构造函数注入
        /// </summary>
        /// <param name="jwtService"></param>
        public AuthenticationController(IJWTService jwtService, WebEnterpriseIIContext db, IUserService userService)
        {
            _jwtService = jwtService;
            _db = db;
            _userService = userService;
        }
        /// <summary>
        /// 创建token
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public string CreateToken(string name,int userid,string email,string nick)
        {
            return _jwtService.CreateToken(name,userid,email,nick);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        [HttpGet]//请求方式
        [Authorize]//授权标识，需要获取授权之后才可以使用
        public string UserInfo()
        {
            //获取用户信息
            return Response.HttpContext.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.Name).Value;
        }

        /// <summary>
        /// 用户注册
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <param name="salt"></param>
        /// <param name="nickname"></param>
        /// <param name="userlevel"></param>
        /// <param name="createtime"></param>
        /// <param name="email"></param>
        /// <returns></returns>
        //用户注册
        [HttpPost]
        public string Registered(string username, string password, byte salt, string nickname, int userlevel, string createtime = null, string email = null)
        {
            //User user = new Models.Database.User();
            //user.UserName = username;
            //user.Password = password;
            //user.Salt = salt;
            //user.Email = email;
            //user.NickName = nickname;
            //User users = _db.Users.FirstOrDefault(x => x.UserName == username);
            var user = _userService.AddUser(username, password, salt, nickname, userlevel, createtime, email);
            var users = _userService.GetUserByUserName(username);
            if (users == null) {
                _db.Users.Add(user);
                if (_db.SaveChanges() > 0) {
                    return "添加成功";
                }
                else {
                    return "添加失败";
                }
            }
            else {
                return "已存在该用户";
            }



        }


        //用户登录
        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        [HttpPost]
        public string login(string username, string password)
        {
            //User user = _db.Users.FirstOrDefault(x => x.UserName == username);
            var user = _userService.GetUserByUserName(username);
            if (user == null) {
                return "用户不存在";
            }
           
            //if (users == null) {
            //    return "密码错误";
            //}
            //else {
            //    return CreateToken(username);
            //}
            if (user.Password != password) {
                //如果不相等 返回密码不正确
                return "密码不正确";
            }
            //如果相等呢？？？
            //创建Token 返回token给前端
            return _jwtService.CreateToken(user.UserName, user.UserId,user.Email,user.NickName);
        }




        //缓存
        [ResponseCache(Duration =10)]
        [HttpGet]
        public string gettime()
        {
            return System.DateTime.Now.ToString();
        }
    }
}
